package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.yanchangapp.R;
import com.zxl.baselib.util.ui.UIUtils;

/**
 * Created by apple on 2019-10-12.
 * description: 设置期限时间dialog
 */
public class SetDieTimeDialog extends Dialog {

    public SetDieTimeDialog(@NonNull Context context) {
        this(context,0);
    }

    public SetDieTimeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    @Override
    public void dismiss() {
        InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 即使当前焦点不在editText，也是可以隐藏的。
        if (imm != null) {
            final View currentFocus = getCurrentFocus();
            IBinder windowToken = null;
            if (currentFocus != null) {
                windowToken = currentFocus.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
        }
        super.dismiss();
    }

    public interface DieTimeEnsureListener {
        void onEnsureClick(int dieTime);
    }

    public static class Builder{
        private Integer time;
        private Context mContext;
        private DieTimeEnsureListener mDieTimeEnsureListener;

        public Builder(Context context){
            mContext = context;
        }

        public Builder setPrice(Integer time){
            this.time = time;
            return this;
        }

        public Builder setEnsureListener(DieTimeEnsureListener ensureListener){
            this.mDieTimeEnsureListener = ensureListener;
            return this;
        }

        public SetDieTimeDialog build(){
            final SetDieTimeDialog dieTimeDialog = new SetDieTimeDialog(mContext, R.style.MyDialog);
            View localView =  LayoutInflater.from(mContext).inflate(R.layout.dialog_set_die_time,null);
            TextView mTvCancel =  localView.findViewById(R.id.tvCancel);
            TextView mTvEnsure = localView.findViewById(R.id.tvEnsure);
            EditText etNum = localView.findViewById(R.id.etNum);
            if (time != null){
                String strTime = String.valueOf(time);
                etNum.setText(strTime);
                etNum.setSelection(strTime.length());
            }
            mTvCancel.setOnClickListener(v -> dieTimeDialog.dismiss());

            mTvEnsure.setOnClickListener(v -> {
                String days = etNum.getText().toString();
                if (TextUtils.isEmpty(days)){
                    UIUtils.showToast("请设置期限天数");
                    return;
                }else {
                    if (Integer.parseInt(days) == 0){
                        UIUtils.showToast("输入有效天数");
                        etNum.setText("");
                        return;
                    }
                }
                if (mDieTimeEnsureListener == null){
                    dieTimeDialog.dismiss();
                }else {
                    mDieTimeEnsureListener.onEnsureClick(Integer.parseInt(days));
                }
                // 关闭弹窗...
                dieTimeDialog.dismiss();
            });
            dieTimeDialog.setContentView(localView);
            return dieTimeDialog;
        }
    }
}
